package com.flainsky.nirvanaplayer;

import com.flainsky.nirvanaplayer.globe.Env;
import com.flainsky.nirvanaplayer.globe.ToastMaker;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;

public class ConfigerActivity extends Activity implements OnClickListener {
	
	EditText et_addr;
	EditText et_server;
	Button bt_ok;
	Button bt_cancel;
	Env env;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.configer_activity);
        
        env = Env.getInstance();
        
        findViews();
        addListeners();
        init();        
	}
	
	private void findViews()
	{
		et_server = (EditText)findViewById(R.id.setting_server_et);
		et_addr = (EditText)findViewById(R.id.setting_addr_et);
		bt_ok = (Button)findViewById(R.id.setting_ok_bt);
		bt_cancel = (Button)findViewById(R.id.setting_cancel_bt);
	}
	
	private void addListeners()
	{
		bt_ok.setOnClickListener(this);
		bt_cancel.setOnClickListener(this);		
	}
	
	private void init()
	{
		et_addr.setText(env.getServiceAddress());
		et_server.setText(env.getServerAddress());
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId())
		{
		case R.id.setting_ok_bt:
			String url = et_addr.getText().toString().trim();
			String serverIp = et_server.getText().toString().trim();
			if(url.equals("")||serverIp.equals(""))
			{
				ToastMaker.showToastLong(ConfigerActivity.this, getString(R.string.no_empty));
				break;
			}
			else
			{
				env.setServiceSharedPreference(url);
				env.setServerSharedPreference(serverIp);
				ToastMaker.showToastLong(ConfigerActivity.this, getString(R.string.save_ok));
				System.exit(0);
				this.finish();
			}
			break;
		case R.id.setting_cancel_bt:
			this.finish();
			break;
		}
	}
}
